// pages/needetile/needetile.js
const app = getApp()
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
var util = require('../../utils/util.js');
Page({
  data: {
    infor:[],
    uptime:'',
    hascur:false, //收藏判断
    hascursigin:false, //报名判断
    id:'',
    signtext:'',
    demand_number_check:'',
    disabled:false,
    page:1,
    datalist:[],
    limit:10,
    nodata: true,
    nodate: true,
    hasdemand_id: '',
    career:'',
    career_company:'',
    checkbm:''
  },
  demand_apply: function (page, limit, user_id, more) {
    var that = this;
    const data = {
      page: page,
      limit: limit,
      demand_id: user_id
    }

    http.request({
      url: 'mini/v1/personal/demand_apply',
      data: data,
      success: function (res) {
        wx.stopPullDownRefresh()
        var data = res.data.data
        for (var i = 0; i < data.length; i++) {
          data[i].created_at = util.timestampToTime(data[i].created_at);
        }

        if (!!more) {
          that.setData({
            datalist: that.data.datalist.concat(res.data.data),
            nodate: true
          })

          if (res.data.data.length == 0) {
            that.setData({
              nodata: false
            });
          }
        } else {
          that.setData({
            datalist: res.data.data,
            nodate: true
          })

          if (res.data.data.length == 0) {
            that.setData({
              nodate: false
            });
          }
        }
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      id:options.id,
      demand_number_check: options.checkup,
      checkbm:wx.getStorageSync('checkrole') == 'boss' ? true : false
    })

    console.log(wx.getStorageSync('checkrole'))

    //已报名人员
      if(!!options.checkup){
        this.demand_apply(1,10,this.data.id);
      }
    //需求详情
    const data = {
      id: options.id
    }
    var that = this;
    http.request({
      url: 'mini/v1/index/demandinfo',
      data: data,
      success: function (res) {
        that.setData({
          infor:res.data,
          career:res.data.career,
          career_company:res.data.career_company,
          uptime: util.timestampToTime(res.data.created_at)
        })

        if (res.data.is_end == 1){
          that.setData({
            disabled:true
          })
        }
      //是否报名
        if (res.data.is_signup == 1){
          that.setData({
            hascursigin: true
          })
        }
      //是否收藏
        if (res.data.is_collect == 1){
          that.setData({
            hascur: true
          })
        }
      }
    })
  },
  onReachBottom: function () {
    if (!!this.data.demand_number_check && this.data.datalist.length !== 0){
      this.setData({
        page: this.data.page + 1
      })
      this.demand_apply(this.data.page, this.data.limit, this.data.id, 'more');
    }
  },
 //收藏
  collect:function(){
    this.setData({
      hascur: !this.data.hascur
    })

    const data = {
      id:this.data.id
    }

    var that = this;
    http.request({
      url: this.data.hascur == false ? 'mini/v1/release/collect_off' : 'mini/v1/release/collect_on',
      data: data,
      methods:'POST',
      success: function (res) {
      }
    })
  },
  //报名
  signup: function () {
    this.setData({
      hascursigin: !this.data.hascursigin
    })

    const data = {
      id: this.data.id
    }

    var that = this;
    http.request({
      url: this.data.hascursigin == false ? 'mini/v1/release/signup_off' : 'mini/v1/release/signup_on',
      data: data,
      methods: 'POST',
      success: function (res) {
      }
    })
  },
  onShareAppMessage: function () {
    return util.share()
  }
})